package com.hyh.baowenlioaworking.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyh.baowenlioaworking.commom.Constants;
import com.hyh.baowenlioaworking.commom.R;
import com.hyh.baowenlioaworking.model.Employee;
import com.hyh.baowenlioaworking.model.dtos.EmployeeDto;
import com.hyh.baowenlioaworking.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @BelongsPackage: com.hyh.baowenlioaworking.controller
 * @ClassName EmployeeController
 * @Author: hyhWTX
 * @CreateTime: 2023-07-21  13:59
 * @Description: TODO (一句话描述以下该类的功能)
 * @Version: 1.0
 */
@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("/query")
    public List<EmployeeDto> query() {
        return employeeService.queryEmployee();
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/queryById/{id}")
    public R queryById(@PathVariable Integer id) {
        Employee employee = employeeService.getById(id);
        if (employee == null) {
            return R.error(Constants.CODE_400, "id不存在，查询失败");
        }
        return R.success(Constants.CODE_200, employee);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public R<Integer>  deleteEmployee(@PathVariable int id) {
        return employeeService.deleteEmployeeById(id);
    }

    /**
     * 清空表数据
     *
     * @return
     */
    @DeleteMapping("/deleteall")
    public R deleteAll() {
        int i = employeeService.deleteAll();
        if (i == 0) {
            return R.error(Constants.CODE_600, "清空失败~");
        }
        return R.success();
    }


    /**
     * 修改操作
     *
     * @param employeeDto
     * @return
     */
    @PostMapping("/update/{id}")
    public R<String> update(@RequestBody EmployeeDto employeeDto) {
//        employeeService.updateById(employee);
        employeeService.updateEmployee(employeeDto);
        return R.success(Constants.CODE_200, "修改成功！！！");
    }

    /**
     * 新增操作
     *
     * @param employeeDto
     * @return
     */
    @PostMapping("/save")
    public R<String> save(@RequestBody EmployeeDto employeeDto) {
//        employeeService.save(employee);
//        return R.success(Constants.CODE_200, "新增成功！！！");
        return employeeService.addEmployee(employeeDto);

    }

    /**
     * 分页
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Page<Employee> page(@RequestParam(defaultValue = "2") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize) {

        return employeeService.getEmployeesPage(pageNum, pageSize);
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {
        Employee employeeyId = employeeService.getById(id);
        log.info("employeeyId"+employeeyId);
        if (employeeyId != null) {
            return R.success(Constants.CODE_200, employeeyId);
        }
        return R.error(Constants.CODE_500, "查无此员工！");
    }

    @RequestMapping("/logout")
    public R<String> logOut(HttpServletRequest request) {
        try {
            //移除存入session中的employee
            request.getSession().removeAttribute("employee");
        } catch (Exception e) {
            return R.error(Constants.CODE_500, "退出失败！");
        }
        return R.success(Constants.CODE_200, "退出成功！");
    }


}
